package com.hurui.xiaoming.http;

import com.hurui.xiaoming.MyApplication;
import com.hurui.xiaoming.http.cookie.AddCookiesInterceptor;
import com.hurui.xiaoming.http.cookie.SaveCookiesInterceptor;
import com.hurui.xiaoming.utils.DefultData;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/7/10.
 */

public class ApiClient {

    private ApiSevice apiSevice ;

    public static ApiClient getModel(){
        return ApiClientHolder.userModel;
    }
    //内部类实现单例模式，延迟加载，线程安全（java中class加载时互斥的）,也减少了内存消耗
    private static class ApiClientHolder{
        private static ApiClient userModel = new ApiClient();//单例对象实例
    }

    private ApiClient(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder() ;

        builder.addInterceptor(new AddCookiesInterceptor(MyApplication.myapp));
        builder.addInterceptor(new SaveCookiesInterceptor(MyApplication.myapp));
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(DefultData.BASEURL)
                //转换服务器数据到对象
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiSevice = retrofit.create(ApiSevice.class);

    }

    public ApiSevice getService(){
        return apiSevice;
    }

}
